package com.cqie.teacherserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.cqie.teacherserver.entity.TeacherAwardsInfo;
import com.cqie.teacherserver.mapper.TeacherAwardsInfoMapper;
import com.cqie.teacherserver.service.ITeacherAwardsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.teacherserver.entity.vo.TeacherAwardsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 * 教职工获奖信息 服务实现类
 * </p>
 *
 * @author zhangr132
 * @since 2024-05-31
 */
@Service
public class TeacherAwardsInfoServiceImpl extends ServiceImpl<TeacherAwardsInfoMapper, TeacherAwardsInfo> implements ITeacherAwardsInfoService {
    @Autowired
    private TeacherAwardsInfoMapper teacherAwardsInfoMapper;


    @Override
    public R getAwardsByTeacher(Serializable gh) {
        try {
            ExecutorService executorService = Executors.newCachedThreadPool();
//        查找奖品数量
            CompletableFuture<Integer> sumAwards = CompletableFuture.supplyAsync(
                    () -> teacherAwardsInfoMapper.selectCount(new LambdaQueryWrapper<TeacherAwardsInfo>().eq(TeacherAwardsInfo::getGh, gh)), executorService);
//        奖品名称
            CompletableFuture<List<TeacherAwardsInfo>> nameAwards = CompletableFuture.supplyAsync(
                    () -> list(new LambdaQueryWrapper<TeacherAwardsInfo>().eq(TeacherAwardsInfo::getGh, gh)), executorService);
            CompletableFuture.allOf(sumAwards, nameAwards).join();
            return R.ok(new TeacherAwardsVO(sumAwards.join().longValue(), nameAwards.join()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.failed("获取失败");
    }

}
